package com.xin.chaingeneration.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xin.chaingeneration.common.ResponseEntity;
import com.xin.chaingeneration.entity.Composition;
import com.xin.chaingeneration.service.CompositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/composition")
public class CompositionController {

    @Autowired
    CompositionService compositionService;

    @PostMapping("/saveBatch")
    public ResponseEntity<String> saveBatch(@RequestBody List<Composition> compositions){
        for (Composition composition : compositions) {
            log.info(composition.toString());
        }
        compositionService.saveBatch(compositions);
        return ResponseEntity.success("保存成功");
    }

    @DeleteMapping("/deleteByProductId")
    public ResponseEntity<String> deleteByProductId(@RequestParam Long productId){
        LambdaQueryWrapper<Composition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Composition::getProductId,productId);

        compositionService.remove(queryWrapper);
        return ResponseEntity.success("删除成功");

    }

    @DeleteMapping("/deleteByProductId/{productId}")
    public ResponseEntity<String> deleteByProductIdv2(@PathVariable Long productId){
        LambdaQueryWrapper<Composition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Composition::getProductId,productId);

        compositionService.remove(queryWrapper);
        return ResponseEntity.success("删除成功");

    }

    @GetMapping("/getByProductId/{productId}")
    public ResponseEntity<List<Composition>> getByProductId(@PathVariable Long productId){

        LambdaQueryWrapper<Composition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Composition::getProductId,productId);

        List<Composition> list = compositionService.list(queryWrapper);

        ResponseEntity<List<Composition>> responseEntity = new ResponseEntity<>();
        responseEntity.setData(list);
        responseEntity.setCode(1);
        return responseEntity;
    }

    @GetMapping("/getById/{id}")
    public ResponseEntity<Composition> getById(@PathVariable Long id){
        Composition byId = compositionService.getById(id);

        ResponseEntity<Composition> compositionResponseEntity = new ResponseEntity<>();
        compositionResponseEntity.setData(byId);
        compositionResponseEntity.setCode(1);
        return compositionResponseEntity;
    }

    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity<String> deleteById(@PathVariable String id){
        compositionService.removeById(id);
        return ResponseEntity.success("删除成功");
    }


}
